package com.raising.sc.upload.file.server;

import com.raising.sc.upload.file.server.entity.FileEntity;
import com.raising.sc.upload.file.server.service.UploadFileService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import com.raising.sc.core.util.common.JsonUtils;

/**
 * 文件资源服务
 * @author GaoYuan
 * @date 2018/4/10 下午3:20
 */
@Controller
@RequestMapping("/file")
@EnableAutoConfiguration
public class UploadFileServerController {

    @Autowired
    UploadFileService uploadFileService;

    //获取配置文件中图片的路径
    @Value("${raising.upload.imagesPath}")
    private String mImagesPath;
    @Value("${raising.upload.id}")
    private String uloadId;
    /**
     * 上传文件
     * @author GaoYuan
     * @date 2018/4/10 上午10:10
     */
    @PostMapping(value="/upload")
    @ResponseBody
    public String upload(HttpServletRequest request) {
        FileEntity fileEntity = new FileEntity();
        InputStream input = null;
        OutputStream output = null;
        try{
            Part part = request.getPart("uploadfile");
            input = part.getInputStream();
            String urlname = part.getSubmittedFileName();
            //获取文件存储路径
            String savePath = mImagesPath + uloadId + File.separator + urlname;
            /** 构造对象 */
            fileEntity.setUrl(uloadId + File.separator + part.getSubmittedFileName());
            fileEntity.setUrlname(urlname.substring(urlname.lastIndexOf("/")+1,urlname.length()));
            fileEntity.setSuffix(urlname.substring(urlname.lastIndexOf("."),urlname.length()));
            fileEntity.setSize(String.valueOf(part.getSize()));
            fileEntity.setCreateDate(new Date());
            fileEntity.setStatus("1");
            /** 创建文件夹 */
            String folder = savePath.substring(0,savePath.lastIndexOf("/"));
            File fileFolder = new File(folder);
            if(!fileFolder.exists()){
                fileFolder.mkdirs();
            }
            /** 创建文件 */
            output = new FileOutputStream(savePath);
            IOUtils.copy(input, output);

            uploadFileService.save(fileEntity);
            return JsonUtils.jsonFormat(fileEntity);
        }catch (Exception e){
            return null;
        }finally {
            /** 释放资源 */
            if(output != null ){
                try{
                    output.close();
                }catch (Exception e){

                }
            }
            if(input != null ){
                try{
                    input.close();
                }catch (Exception e){

                }
            }
        }

    }




}
